package com.spooky.sync.note.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import com.spooky.sync.R;
import com.spooky.sync.persistence.entity.Category;
import com.spooky.sync.persistence.entity.Note;

public class EditorNoteDialog extends Dialog {
	private static final String TAG = "EditorNoteDialog";
	// GUI
	private EditText mEditText;
	private CheckBox mCheckBox;
	private Spinner mSpinner;
	private SpinnerAdapter mSpinnerAdapter = null;
	private Context mContext;
	private EditText mTextEditor;
	// BUNDLE KEY
	private static final String IS_DYNAMIC_TITLE_KEY = "mIsDynamicTitle";
	private static final String CUSTOM_TITLE_KEY = "mCustomTitle";
	// TMP STORAGE
	private boolean mIsDynamic = true;
	private String mDynamicTitle = null;
	private String mCustomTitle = null;
	protected Note mNote;

	public EditorNoteDialog(Context context, EditText text) {
		super(context);// R.layout.editor_note_dialog
		mContext = context;
		mTextEditor = text;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editor_note_dialog);
		LayoutParams params = getWindow().getAttributes();
		params.height = LayoutParams.FILL_PARENT;
		params.width = LayoutParams.FILL_PARENT;
		getWindow().setAttributes(params);
		mEditText = (EditText) findViewById(R.id.editor_note_dialog_text);
		mCheckBox = (CheckBox) findViewById(R.id.editor_note_dialog_is_dynamic);
		mSpinner = (Spinner) findViewById(R.id.editor_note_dialog_categories);
		mEditText.setText(mCustomTitle == null ? "" : mCustomTitle);
		mCheckBox.setChecked(mIsDynamic);
		mSpinner.setAdapter(mSpinnerAdapter == null ? new ArrayAdapter<Category>(mContext, android.R.layout.simple_spinner_dropdown_item) : mSpinnerAdapter);
		initiate();
	}

	public void onSaveInstanceState(Bundle savedInstanceState) {
		savedInstanceState.putBoolean(IS_DYNAMIC_TITLE_KEY, mIsDynamic);
		savedInstanceState.putString(CUSTOM_TITLE_KEY, mCustomTitle);
	}

	private void initiate() {
		mNote = new Note();
		mEditText.setOnKeyListener(new android.view.View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				Log.d(TAG, "onChangeTitle");
				if (mEditText.length() == 0) {
					mCheckBox.setChecked(true);
				} else {
					mCustomTitle = mEditText.getText().toString();
					mCheckBox.setChecked(false);
				}
				return false;
			}
		});
		mEditText.setSingleLine(true);
		mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(Note.MAX_TITLE_LENGTH) });
		mEditText.setText(mNote.getTitle());
		mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Log.d(TAG, "onChangeIsDynamicTitle");
				mIsDynamic = isChecked;
				if (isChecked) {
					if (!mEditText.isFocused()) {
						if (mTextEditor != null) {
							mEditText.setText(EditorUtil.getDynamicTitle(mTextEditor.getText().toString()));
						}
					}
				} else {
					mEditText.setText(mCustomTitle);
				}
			}
		});
		mCheckBox.setText("Dynamic");
		final CategorySpinner categories = (CategorySpinner) findViewById(R.id.editor_note_dialog_categories);
		mCheckBox.setChecked(mIsDynamic);
	}

	public void onConfirm(Dialog view) {
		Log.d(TAG, "onClickConfirm");
		if (mEditText.isFocused() && mCheckBox.isChecked()) {
			mNote.setTitle((String) EditorUtil.getDynamicTitle(mTextEditor.getText().toString()));
		} else {
			mNote.setTitle(mEditText.getText().toString());
		}
	}

	public void onBack(Dialog view) {
	}

	public void onBack(View view) {
	}

	public void setTitle(String title) {
		mDynamicTitle = title;
	}

	public void setIsDynamic(boolean checked) {
		mIsDynamic = checked;
	}

	public void setCategories(SpinnerAdapter adapter) {
		mSpinnerAdapter = adapter;
	}

	protected static class EditorUtil {
		private static final String[] SPLITTERS = new String[] { "-", "_", "=", ".", ";", "|", ":", "/" };

		protected static CharSequence getDynamicTitle(final String text) {
			if (text == null || text.equals("")) {
				return "";
			}
			String truncatedText = null;
			if (text.length() > Note.MAX_TITLE_LENGTH) {
				truncatedText = text.substring(0, Note.MAX_TITLE_LENGTH);
			} else {
				truncatedText = text;
			}
			int pos = -1;
			for (String splitter : SPLITTERS) {
				pos = text.indexOf(splitter);
				if (pos != -1) {
					String substr = truncatedText.substring(0, pos).trim();
					return substr.length() >= 2 ? substr : truncatedText.trim();
				}
			}
			return truncatedText.trim();
		}
	}
}
